import pygame,sys,os
from MainWindow import MainWindow

class StartMenu(MainWindow):
    def __init__(self):
        super().__init__()

        self.running = True

        # 加载背景图片
        try:
            self.background = pygame.image.load(self.resource_path('source/img/back.png')).convert()
            self.background = pygame.transform.scale(self.background, (self.Screen_width, self.Screen_height))
        except FileNotFoundError:
            print("警告：未找到背景图片，使用纯色背景")
            self.background = pygame.Surface((self.Screen_width, self.Screen_height))
            self.background.fill((30, 30, 100))

        # 颜色定义
        self.WHITE = (255, 255, 255)
        self.BUTTON_COLOR = (70, 130, 180)
        self.BUTTON_HOVER_COLOR = (100, 150, 200)
        self.Start()
        # 字体设置（需要中文字体文件）
        try:
            self.title_font = pygame.font.Font(self.resource_path("source/fonts/峰广明锐体.ttf"), 72)  # 微软雅黑
            self.button_font = pygame.font.Font(self.resource_path("source/fonts/峰广明锐体.ttf"), 40)
        except:
            print("警告：未找到字体文件，使用默认字体")
            self.title_font = pygame.font.Font(None, 72)
            self.button_font = pygame.font.Font(None, 40)

        # 按钮尺寸
        self.button_width = 250  # 加宽按钮适应中文
        self.button_height = 65

        # 初始化界面元素
        self.init_button()

    def init_button(self):
        # 标题文字
        self.title_text = self.title_font.render("门险环生", True, (253, 106, 33))
        self.title_rect = self.title_text.get_rect(center=(self.Screen_width // 2, 250))

        # 开始按钮
        self.start_button = pygame.Rect(
            self.Screen_width // 2 - self.button_width // 2,
            530,
            self.button_width,
            self.button_height
        )
        self.start_text = self.button_font.render("开始游戏", True, self.WHITE)

        # 退出按钮
        self.quit_button = pygame.Rect(
            self.Screen_width // 2 - self.button_width // 2,
            650,
            self.button_width,
            self.button_height
        )
        self.quit_text = self.button_font.render("退出游戏", True, self.WHITE)

    def handle_events(self):
        mouse_pos = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit_game()

            if super().check_mute_click(event):  # 处理静音按钮点击
                continue

            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.start_button.collidepoint(mouse_pos):
                    return "game"
                elif self.quit_button.collidepoint(mouse_pos):
                    self.quit_game()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.quit_game()
                if event.key == pygame.K_RETURN:
                    return "game"

        return "menu"

    def draw(self):
        # 绘制背景
        self.window.blit(self.background, (0, 0))
        # 绘制标题
        self.window.blit(self.title_text, self.title_rect)
        # 绘制开始按钮
        mouse_pos = pygame.mouse.get_pos()
        start_color = self.BUTTON_HOVER_COLOR if self.start_button.collidepoint(mouse_pos) else self.BUTTON_COLOR
        pygame.draw.rect(self.window, start_color, self.start_button, border_radius=10)
        self.window.blit(self.start_text,
                         (self.start_button.centerx - self.start_text.get_width() // 2,
                          self.start_button.centery - self.start_text.get_height() // 2))

        # 绘制退出按钮
        quit_color = self.BUTTON_HOVER_COLOR if self.quit_button.collidepoint(mouse_pos) else self.BUTTON_COLOR
        pygame.draw.rect(self.window, quit_color, self.quit_button, border_radius=10)
        self.window.blit(self.quit_text,
                         (self.quit_button.centerx - self.quit_text.get_width() // 2,
                          self.quit_button.centery - self.quit_text.get_height() // 2))

        self.draw_mute_button()  # 绘制静音按钮
        pygame.display.update()

    def quit_game(self):
        pygame.quit()
        sys.exit()

    def run(self):
        while self.running:
            action = self.handle_events()
            if action != "menu":
                return action
            self.draw()
            pygame.display.update()
            pygame.time.Clock().tick(60)
    def resource_path(self,relative_path):
        """ 根据打包后的环境动态加载资源路径 """
        if hasattr(sys, '_MEIPASS'):
            base_path = sys._MEIPASS
        else:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)

